package com.example.jcdev1.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;

import com.wordplat.ikvstockchart.InteractiveKLineLayout;
import com.wordplat.ikvstockchart.KLineHandler;
import com.wordplat.ikvstockchart.entry.Entry;
import com.wordplat.ikvstockchart.entry.EntrySet;

import java.util.Random;

public class MainActivity extends AppCompatActivity {


    InteractiveKLineLayout kLineLayout;
    private EntrySet entrySet = new EntrySet();
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        kLineLayout = (InteractiveKLineLayout) findViewById(R.id.klinelayout);

        getDatas();
        setKHandler();
//        initTimerListener();
//        handler.post(runnable);
    }

    /**
     * 获取数据
     */
    private void getDatas() {
        //hello test2
        float open,close,hight,low;
        for (int i = 0; i < 30; i++) {
            open = getRadom(30, 10);
            close = getRadom(30, 10);
            if (open > close) {
                hight = open + getRadom(10, 0);
                low = close - getRadom(10, 0);
            } else {
                hight = close + getRadom(10, 0);
                low = open - getRadom(10, 0);
            }

            entrySet.addEntry(new Entry(open, hight, low, close, (int) getRadom(100, 50), "2017-04-18"));
        }

        kLineLayout.getKLineView().setEntrySet(entrySet);
        kLineLayout.getKLineView().notifyDataSetChanged();
        kLineLayout.showMACD();


    }

    /**
     * 设置K线图状态监听
     */
    private void setKHandler() {
        kLineLayout.getKLineView().setKLineHandler(new KLineHandler() {
            @Override
            public void onLeftRefresh() {
                getDatas();

                kLineLayout.getKLineView().refreshComplete();
            }

            @Override
            public void onRightRefresh() {
                getDatas();
                kLineLayout.getKLineView().refreshComplete();

            }

            @Override
            public void onSingleTap(MotionEvent e, float x, float y) {

            }

            @Override
            public void onDoubleTap(MotionEvent e, float x, float y) {

            }

            @Override
            public void onHighlight(Entry entry, int entryIndex, float x, float y) {

            }

            @Override
            public void onCancelHighlight() {

            }
        });
    }

    /**
     * 设置定时器
     */
    Runnable runnable;

    private void initTimerListener() {
        runnable = new Runnable() {
            @Override
            public void run() {
                getDatas();
                handler.postDelayed(this, 1000);
            }
        };
    }

    /**
     * 获取一定范围内随机数
     *
     * @param max
     * @param min
     * @return
     */
    private float getRadom(int max, int min) {

        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;
    }
}
